/*
 * Copyright 2011 Mark Davidson.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package org.dctmcommons;

import org.dctmcommons.impl.SecurityHelper;
import org.junit.*;

import com.documentum.fc.client.IDfACL;
import com.documentum.fc.common.DfDocbaseConstants;
import com.documentum.fc.common.DfException;

import static org.junit.Assert.*;
import static org.hamcrest.Matchers.*;

/**
 * Integration tests for {@link SecurityHelper}.
 */
public class SecurityHelperIT {

	/** Serial id. */
	private static final long serialVersionUID = 5004420999557810635L;
	
	/** the name of a known ACL. */
	private static final String KNOWN_ACL_NAME = "replica_acl_default";

	/** The security helper instance. */
	private ISecurityHelper securityHelper = null;

	/**
	 * Tests the factory method.
	 */
	@Test
	public void testGetInstance() {
		System.out.println(">>> testGetInstance");
		assertThat("Factory method returned null",
				   securityHelper,
				   is(notNullValue()));
	}

	/**
	 * Tests the isFolderSecurityEnabled method.
	 * @throws DfException The repository exception
	 */
	@Test
	public void testIsFolderSecurityEnabled() throws DfException {
		System.out.println(">>> testIsFolderSecurityEnabled");
		
		assertThat("Folder security shold be reported as disabled",
				   securityHelper.isFolderSecurityEnabled(
						   DctmCommonsTestUtil.getSession()),
				   is(true));
		
		DctmCommonsTestUtil.releaseSession();
	}
	
	/**
	 * Tests retrieval of ACLs - by name.
	 */
	@Test
	public void testGetAclByName() throws DfException {
		System.out.println(">>> testGetAclByName");
		final IDfACL template = securityHelper.getAclByName(
											DctmCommonsTestUtil.getSession(),
											KNOWN_ACL_NAME);
		assertThat("Unable to get ACL by name: " + KNOWN_ACL_NAME,
				   template,
				   is(notNullValue()));
		
		DctmCommonsTestUtil.releaseSession();
	}
	
	/**
	 * Tests retrieval of ACLs - by qualification.
	 */
	@Test
	public void testGetAclByQualification() throws DfException {
		System.out.println(">>> testGetAclByQualification");
		final String qual = DfDocbaseConstants.OBJECT_NAME + " = '" +
													KNOWN_ACL_NAME + "'";
		final IDfACL acl = securityHelper.getAclByQualification(
											DctmCommonsTestUtil.getSession(),
											qual);
		assertThat("Unable to get ACL by qual: " + KNOWN_ACL_NAME,
				   acl,
				   is(notNullValue()));
		
		DctmCommonsTestUtil.releaseSession();
	}
	
	/**
	 * Setup routine.
	 */
	@Before
	public void before() throws DfException {
		securityHelper = SecurityHelper.getInstance();
	}
	
	/**
	 * Teardown routine.
	 */
	@After
	public void after() throws DfException {
	}
}
